package clem;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.AbstractCellEditor;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

public class MainWindow2 extends JFrame {
  private static final long serialVersionUID = 1L;
  private JTable tblDati;
  
  public MainWindow2() {
    super("CRUD");
    inizializzaComponenti();
  }
  
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        new MainWindow2().setVisible(true);
      }
    });
    
  }
  
  private void inizializzaComponenti() {
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
    tblDati  = new JTable(new Object[][]{{"A","B"},{"A","B"}},new Object[]{"1","2"});;
    tblDati.setTableHeader(new EditableJTableHeader(tblDati.getColumnModel()));
    
    EditableHeaderTableColumn col = (EditableHeaderTableColumn) tblDati.getColumnModel().getColumn(1);
    
    class ButtonEditor extends AbstractCellEditor implements TableCellEditor {
      private JButton btn;
      public ButtonEditor(){
        btn = new JButton();
        btn.setText("A");
      }
      @Override
      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return btn;
      }

      @Override
      public Object getCellEditorValue() {
        return null;
      }
      
    }      
    col.setHeaderEditor(new ButtonEditor());
    
    JScrollPane scrollPane = new JScrollPane(tblDati);
    setLayout(new BorderLayout());
    add(scrollPane, BorderLayout.CENTER);
    scrollPane.setViewportView(tblDati);
    scrollPane.setPreferredSize(new Dimension(200,200));
    pack();
  }
  
}
